package leetcode.editor.vscdev.greedy;

public class lc860 {
    
}
class Solution {
    public boolean lemonadeChange(int[] bills) {
        return func1(bills);
    }

    private boolean func1(int[] bills) {
        // 维护三种类型的票子
        int five = 0, ten = 0, twenty = 0;
        // 是5直接收下,10返回一张五,20返回一张五加一张十或者三张五.
        for (int i = 0; i < bills.length; i++) {
            if (bills[i] == 5) five++;
            else if (bills[i] == 10) {
                if (five == 0) return false;
                five--;
                ten++;
            } else {
                // 优先消耗十元,因为五元作用更大(找零多次使用)
                if (five > 0 && ten > 0) {
                    five--;
                    ten--;
                } else if (five >= 3) {
                    five -= 3;
                } else {
                    return false;
                }
                twenty++;
            }
        }
        return true;
    }
}